﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Imaginecup.stage;
using Microsoft.Xna.Framework.Content;
using Imaginecup.shareData;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.util;
using Imaginecup.gameState.malaria.stage;
using Imaginecup.util.collision;
using Imaginecup.sprite;
using imaginecup.util;
using Imaginecup.util.resourceMan;
using Imaginecup.factory;
using Imaginecup.gameState;

namespace Imaginecup.stage
{
    public class ChoiceStage:IStageLoop , IStage
    {
        private IStageManController m_StageMan;

        private static ChoiceStage m_ChoiceStage = null;

        private ISprite m_BackSprite;
        private ISprite m_BackFrame;
        private ISprite m_ExitSprite;

        private EntityInfo m_BackInfo;

        private EntityInfo m_ExitInfo;

        private ChoiceStage()
        {

            m_BackInfo = new EntityInfo(0, 0, 800, 480);
            m_ExitInfo = new EntityInfo(724, 24, 48, 50);
            
            NormalSprite sprite = new NormalSprite();
            sprite.SetTextureName("Select_Back");

            m_BackSprite = sprite;

            sprite = new NormalSprite();
            sprite.SetTextureName("Select_Frame");

            m_BackFrame = sprite;

            sprite = new NormalSprite();
            sprite.SetTextureName("Exit");

            m_ExitSprite = sprite;
        }

        public static ChoiceStage GetChoiceStage()
        {
            if( m_ChoiceStage == null )
            {
                m_ChoiceStage = new ChoiceStage();
            }
            return m_ChoiceStage;
        }

        public void SetStageMan(IStageManController stageMan)
        {
            m_StageMan = stageMan;
        }
        
        public void Release()
        {
            m_BackFrame.Release();
            m_BackSprite.Release();
            m_ExitSprite.Release();

            m_ChoiceStage = null;
        }

        public void Update(GameTime gameTime)
        {
            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

            if (mouseInput.IsOneClickUp())
            {
                for( int i = 0 ; m_StageMan.HasNext( i ) ; i++ )
                {
                    StageEnum stageEnum = m_StageMan.GetStageEnum(i);
                    ChoiceBox choiceBox = m_StageMan.GetChoiceBox(stageEnum);

                    if( choiceBox.GetEnable() && Collision.CollisionByPoint( choiceBox.GetEntityInfo() , mouseInput.Position))
                    {
                        m_StageMan.ChangeStage(stageEnum);
                    }
                }

                if( Collision.CollisionByPoint(m_ExitInfo , mouseInput.Position) )
                {
                    StateShareData.GetShareData().GetGameStateMan().PopState();
                    StateShareData.GetShareData().GetGameStateMan().PushState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_ChoiceMap) );
                    return;
                }
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            m_BackSprite.Draw(spriteBatch, m_BackInfo);
            m_BackFrame.Draw(spriteBatch, m_BackInfo);
            m_ExitSprite.Draw(spriteBatch, m_ExitInfo);

            for( int i = 0 ; m_StageMan.HasNext(i); i++ )
            {
                StageEnum stageEnum = m_StageMan.GetStageEnum(i);
                ChoiceBox choiceBox = m_StageMan.GetChoiceBox(stageEnum);

                choiceBox.Draw(spriteBatch);
            }
        }
    }
}
